﻿package Cain.bulanMove
{
	import Cain.bulanMove.BulanMovePoint;
	import flash.display.DisplayObject;
	import flash.events.Event;
	/**
	 * 布朗隨機運動物件 v1.2 2010.10.5
	 * @author Cain@2009 in ADCAST
	 */
	public class BulanMoveMC
	{
		private var vv:Number;
		private var bb:BulanMovePoint;
		private var mc:DisplayObject;
		private var vx:Number;
		private var vy:Number;
		/**
		 * 布朗隨機運動物件
		 * @param	_mc 移動物件
		 * @param	_v 最高速度動量
		 */
		public function BulanMoveMC(_mc:DisplayObject,_v:Number = .5):void 
		{
			mc = _mc;
			vv = _v;
			bb = new BulanMovePoint(mc.x, mc.y, vv);
			playit();
		}
		/**
		 * 開始移動
		 * @param	_add_x x移動
		 * @param	_add_y y移動
		 */
		public function playit(_add_x:Number = 0,_add_y:Number=0):void 
		{
			vx = _add_x;
			vy = _add_y;
			mc.addEventListener(Event.ENTER_FRAME, gogo);
			mc.addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED);
		}
		private function onREMOVED(e:Event):void 
		{
			mc.removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED);
			if (mc.hasEventListener(Event.ENTER_FRAME)) 
			{
				mc.removeEventListener(Event.ENTER_FRAME, gogo);
			}
		}
		
		private function gogo(e:Event):void 
		{
			bb.move(vx,vy);
			mc.x = bb.x;
			mc.y = bb.y;
		}
		/**
		 * 停止移動
		 */
		public function stopit():void 
		{
			mc.removeEventListener(Event.ENTER_FRAME, playit);
		}
		/**
		 * 設定動量
		 */
		public function set v(_n:Number):void 
		{
			bb.v = _n;
		}
		/**
		 * 目前壽命(移動次數)
		 */
		public function get live():int 
		{
			return bb.live;
		}
	
	}
	
}